package utils
{

	/**
	 * 客户端与服务端牌的值相互转换
	 * @author xumin.xu
	 */
	public class TileTransform
	{

		/**
		 * 服务端牌转换为客户端表示形式
		 * @param tileId
		 * @return
		 * 18->"8,2"
		 */
		public static function serverToClient(tileId:int):Object
		{
			var type:int = -1;
			var value:int = 0;
			if (tileId < 10)
			{
				type = 2;
				value = tileId;
			}
			else if (tileId < 20)
			{
				type = 1;
				value = tileId - 10;
			}
			else if (tileId < 30)
			{
				type = 0;
				value = tileId - 20;
			}
			switch (tileId)
			{
				//东
				case 31:
					type = 3;
					break;
				//南
				case 32:
					type = 5;
					break;
				//西
				case 33:
					type = 4;
					break;
				//北
				case 34:
					type = 6;
					break;
				//中
				case 35:
					type = 7;
					break;
				//发
				case 36:
					type = 9;
					break;
				//白
				case 37:
					type = 8;
					break;
				//王
				case 41:
					type = 10;
					break;
			}
			if (type == -1)
			{
				logger("出现异常牌号" + tileId);
			}

			return {value: value, type: type};
		}

		/**
		 * 客户端牌转换为服务端表示形式
		 * "8,2"->18
		 */
		public static function clienToServer(tileObj:Object):int
		{
			var type:int = tileObj.type;
			var value:int = tileObj.value;

			var tileId:int = 0;
			switch (type)
			{
				case 0:
					tileId = 20 + value;
					break;
				case 1:
					tileId = 10 + value;
					break;
				case 2:
					tileId = value;
					break;

				case 3:
					tileId = 31
					break;
				case 4:
					tileId = 33
					break;
				case 5:
					tileId = 32
					break;
				case 6:
					tileId = 34
					break;
				case 7:
					tileId = 35
					break;
				case 8:
					tileId = 37
					break;
				case 9:
					tileId = 36
					break;
				case 10:
					tileId = 41;
					break;
			}
			return tileId;
		}

		/**
		 *  服务端牌列表转换为客户端表示形式
		 * @param arr
		 * @return
		 *
		 */
		public static function arrServerToClient(arr:Array):Array
		{
			var arr1:Array = [];
			var length:int = arr.length;
			for (var i:int = 0; i < length; i++)
			{
				arr1[i] = serverToClient(arr[i]);
			}
			return arr1;
		}

		/** 服务端牌的规则
	   1,1,1,0,1,1,1,0,0,    # tiles[ 1- 9]  壹万～玖万的个数
								  0,
	   0,0,0,0,0,3,0,0,0,    # tiles[11-19]  壹铜～玖铜的个数
								  0,
	   0,0,0,0,0,0,0,0,0,    # tiles[21-29]  壹条～玖条的个数
								  0,
	   0,1,1,1,0,0,0         # tiles[31-37]  东南西北中发白的个数
		  * 						tiles[41]	王
							*
	   例：	15：五饼
						26：六条
			*
		 **/

		/** 客户端牌规则 value,type
		   type:牌的类型  0、条	1、筒 2、万  3~10 东西南北中白发 王
		   value:牌的大小

		 例： 	4-0:四万
			8-1：八筒
				0-8：发财
		*/

		/**
		 * 客户端牌比较函数
		 */		
		public static function sortOnClientTile(a:Object, b:Object):Number
		{
			var tileA:int = TileTransform.clienToServer(a);
			var tileB:int = TileTransform.clienToServer(b);
			if (tileA > tileB)
			{
				return 1;
			}
			else if (tileA < tileB)
			{
				return -1;
			}
			else
			{
				return 0;
			}
		}
		
		/**
		 * 服务端牌比较函数
		 */		
		public static function sortOnServerTile(tileA:int, tileB:int):Number
		{
			if (tileA > tileB)
			{
				return 1;
			}
			else if (tileA < tileB)
			{
				return -1;
			}
			else
			{
				return 0;
			}
		}

	}
}
